home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / FieldView.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  256 lines

  1. /*
  2.  * @(#)FieldView.java    1.9 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.awt.*;
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.event.*;
  25.  
  26. /**
  27.  * Extends the multi-line plain text view to be suitable
  28.  * for a single-line editor view.  If the view is 
  29.  * allocated extra space, the field must adjust for it.
  30.  * If the hosting component is a JTextField, this view
  31.  * will manage the ranges of the associated BoundedRangeModel
  32.  * and will adjust the horizontal allocation to match the
  33.  * current visibility settings of the JTextField.
  34.  *
  35.  * @author  Timothy Prinzing
  36.  * @version 1.9 04/09/98
  37.  * @see     View
  38.  */
  39. public class FieldView extends PlainView {
  40.  
  41.     /**
  42.      * Constructs a new FieldView wrapped on an element.
  43.      *
  44.      * @param elem the element
  45.      */
  46.     public FieldView(Element elem) {
  47.     super(elem);
  48.     }
  49.  
  50.     /**
  51.      * Fetches the font metrics associated with the component hosting
  52.      * this view.
  53.      *
  54.      * @return the metrics
  55.      */
  56.     protected FontMetrics getFontMetrics() {
  57.     Component c = getContainer();
  58.     return c.getFontMetrics(c.getFont());
  59.     }
  60.  
  61.     /**
  62.      * Adjusts the allocation given to the view
  63.      * to be a suitable allocation for a text field.
  64.      * If the view has been allocated more than the 
  65.      * preferred span vertically, the allocation is
  66.      * changed to be centered vertically.  Horizontally
  67.      * the view is adjusted according to the horizontal
  68.      * alignment property set on the associated JTextField
  69.      * (if that is the type of the hosting component).
  70.      *
  71.      * @param a the allocation given to the view, which may need
  72.      *  to be adjusted.
  73.      * @return the allocation that the superclass should use.
  74.      */
  75.     protected Shape adjustAllocation(Shape a) {
  76.     if (a != null) {
  77.         Rectangle bounds = a.getBounds();
  78.         int vspan = (int) getPreferredSpan(Y_AXIS);
  79.         int hspan = (int) getPreferredSpan(X_AXIS);
  80.         if (bounds.height != vspan) {
  81.         int slop = bounds.height - vspan;
  82.         bounds.y += slop / 2;
  83.         bounds.height -= slop;
  84.         }
  85.  
  86.         // horizontal adjustments
  87.         Component c = getContainer();
  88.         if (c instanceof JTextField) {
  89.         JTextField field = (JTextField) c;
  90.         BoundedRangeModel vis = field.getHorizontalVisibility();
  91.         vis.setMaximum(Math.max(hspan, bounds.width));
  92.         vis.setExtent(bounds.width - 1);
  93.         if (hspan < bounds.width) {
  94.             // horizontally align the interior
  95.             int slop = bounds.width - 1 - hspan;
  96.             switch (((JTextField)c).getHorizontalAlignment()) {
  97.             case SwingConstants.CENTER:
  98.             bounds.x += slop / 2;
  99.             bounds.width -= slop;
  100.             break;
  101.             case SwingConstants.RIGHT:
  102.             bounds.x += slop;
  103.             bounds.width -= slop;
  104.             break;
  105.             }
  106.         } else {
  107.             // adjust the allocation to match the bounded range.
  108.             bounds.width = hspan;
  109.             bounds.x -= vis.getValue();
  110.         }
  111.         }
  112.         return bounds;
  113.     }
  114.     return null;
  115.     }
  116.  
  117.     /**
  118.      * Update the visibility model with the associated JTextField
  119.      * (if there is one) to reflect the current visibility as a
  120.      * result of changes to the document model.  The bounded
  121.      * range properties are updated.  If the view hasn't yet been
  122.      * shown the extent will be zero and we just set it to be full
  123.      * until determined otherwise.
  124.      */
  125.     void updateVisibilityModel() {
  126.     Component c = getContainer();
  127.     if (c instanceof JTextField) {
  128.         JTextField field = (JTextField) c;
  129.         BoundedRangeModel vis = field.getHorizontalVisibility();
  130.         int hspan = (int) getPreferredSpan(X_AXIS);
  131.         int extent = vis.getExtent();
  132.         int maximum = Math.max(hspan, extent);
  133.         extent = (extent == 0) ? maximum : extent;
  134.         int value = maximum - extent;
  135.         vis.setRangeProperties(value, extent, 0, maximum, false);
  136.     }
  137.     }
  138.  
  139.     // --- View methods -------------------------------------------
  140.  
  141.     /**
  142.      * Renders using the given rendering surface and area on that surface.
  143.      * The view may need to do layout and create child views to enable
  144.      * itself to render into the given allocation.
  145.      *
  146.      * @param g the rendering surface to use
  147.      * @param a the allocated region to render into
  148.      *
  149.      * @see View#paint
  150.      */
  151.     public void paint(Graphics g, Shape a) {
  152.     super.paint(g, adjustAllocation(a));
  153.     }
  154.  
  155.     /**
  156.      * Determines the preferred span for this view along an
  157.      * axis.
  158.      *
  159.      * @param axis may be either View.X_AXIS or View.Y_AXIS
  160.      * @returns  the span the view would like to be rendered into >= 0.
  161.      *           Typically the view is told to render into the span
  162.      *           that is returned, although there is no guarantee.  
  163.      *           The parent may choose to resize or break the view.
  164.      */
  165.     public float getPreferredSpan(int axis) {
  166.     switch (axis) {
  167.     case View.X_AXIS:
  168.         Segment buff = getLineBuffer();
  169.         Document doc = getDocument();
  170.         int width;
  171.         try {
  172.         doc.getText(0, doc.getLength(), buff);
  173.         width = Utilities.getTabbedTextWidth(buff, getFontMetrics(), 0, this, 0);
  174.         } catch (BadLocationException bl) {
  175.         width = 0; 
  176.         }
  177.         return width;
  178.     default:
  179.         return super.getPreferredSpan(axis);
  180.     }
  181.     }
  182.  
  183.     /**
  184.      * Determines the resizability of the view along the
  185.      * given axis.  A value of 0 or less is not resizable.
  186.      *
  187.      * @param axis View.X_AXIS or View.Y_AXIS
  188.      * @return the weight -> 1 for View.X_AXIS, else 0
  189.      */
  190.     public int getResizeWeight(int axis) {
  191.     if (axis == View.X_AXIS) {
  192.         return 1;
  193.     }
  194.     return 0;
  195.     }
  196.  
  197.     /**
  198.      * Provides a mapping from the document model coordinate space
  199.      * to the coordinate space of the view mapped to it.
  200.      *
  201.      * @param pos the position to convert >= 0
  202.      * @param a the allocated region to render into
  203.      * @return the bounding box of the given position
  204.      * @exception BadLocationException  if the given position does not
  205.      *   represent a valid location in the associated document
  206.      * @see View#modelToView
  207.      */
  208.     public Shape modelToView(int pos, Shape a) throws BadLocationException {
  209.     return super.modelToView(pos, adjustAllocation(a));
  210.     }
  211.  
  212.     /**
  213.      * Provides a mapping from the view coordinate space to the logical
  214.      * coordinate space of the model.
  215.      *
  216.      * @param fx the X coordinate >= 0.0f
  217.      * @param fy the Y coordinate >= 0.0f
  218.      * @param a the allocated region to render into
  219.      * @return the location within the model that best represents the
  220.      *  given point in the view
  221.      * @see View#viewToModel
  222.      */
  223.     public int viewToModel(float fx, float fy, Shape a) {
  224.     return super.viewToModel(fx, fy, adjustAllocation(a));
  225.     }
  226.  
  227.     /**
  228.      * Gives notification that something was inserted into the document
  229.      * in a location that this view is responsible for.
  230.      *
  231.      * @param changes the change information from the associated document
  232.      * @param a the current allocation of the view
  233.      * @param f the factory to use to rebuild if the view has children
  234.      * @see View#insertUpdate
  235.      */
  236.     public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
  237.     super.insertUpdate(changes, adjustAllocation(a), f);
  238.     updateVisibilityModel();
  239.     }
  240.  
  241.     /**
  242.      * Gives notification that something was removed from the document
  243.      * in a location that this view is responsible for.
  244.      *
  245.      * @param changes the change information from the associated document
  246.      * @param a the current allocation of the view
  247.      * @param f the factory to use to rebuild if the view has children
  248.      * @see View#removeUpdate
  249.      */
  250.     public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
  251.     super.removeUpdate(changes, adjustAllocation(a), f);
  252.     updateVisibilityModel();
  253.     }
  254.  
  255. }
  256.